package com.wangyadong.hobby.javanetwork.input;

import java.io.*;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/517:23
 **/
public class InputTest {


    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("D://1.txt"));
        readMultpileAvailable(fileInputStream);
    }

    /**
     * 单个字节读取
     *
     * @param inputStream
     * @throws IOException
     */
    public static void read(InputStream inputStream) throws IOException {
        byte[] input = new byte[10];
        for (int i = 0; i < input.length; i++) {
            int read = inputStream.read();
            if (read == -1) {
                break;
            }
            System.out.println(read);
            input[i] = (byte) read;
        }
    }

    public static void readMultpile(InputStream inputStream) throws IOException {
        int byteRead = 0;
        int bytesToRead = 1024;
        byte[] input = new byte[bytesToRead];
        while (byteRead < bytesToRead) {
            byteRead += inputStream.read(input, byteRead, bytesToRead - byteRead);
//            if (read == -1) break;
            System.out.println(byteRead);
        }
    }


    public static void readMultpileFixBug(InputStream inputStream) throws IOException {
        int byteRead = 0;
        int bytesToRead = 1024;
        byte[] input = new byte[bytesToRead];
        while (byteRead < bytesToRead) {
            int read = inputStream.read(input, byteRead, bytesToRead - byteRead);
            if (read == -1) break;
            System.out.println(read);
            byteRead += read;
        }
        System.out.println(new String(input));
    }

    public static void readMultpileAvailable(InputStream inputStream) throws IOException {
        int byteRead = 0;
        int bytesToRead = inputStream.available();
        byte[] input = new byte[bytesToRead];
        int read = inputStream.read(input, byteRead, bytesToRead - byteRead);
        System.out.println(new String(input));
    }
}